package com.gree.flowable.app.mapper.dao;

import com.gree.flowable.app.mapper.CreateTablesMapper;
import com.gree.flowable.app.mapper.FormDataMapper;
import com.gree.flowable.app.tools.utils.DuridConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/4/27 17:12
 * @description：主表数据Dao
 * @modified By：
 * @version: 1.0
 */
@Repository
public class FormDataDao {

    @Autowired
    private DuridConfig duridConfig;

    @Autowired
    private FormDataMapper formDataMapper;

    @Autowired
    private CreateTablesMapper createTablesMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int addFormData(String businessKey, String formId, Map<String,Object> data) throws Exception {
        int result = 0;
        /*Connection conn = null;
        PreparedStatement ps = null;

        String url = duridConfig.getUrl();
        boolean isMysql = url.contains("mysql");
        conn = jdbcTemplate.getDataSource().getConnection();
        StringBuilder columnSql = new StringBuilder("insert into "+formId+" ( business_key ");
        StringBuilder valueSql = new StringBuilder(" values ( '"+businessKey+ "' ");
        for (Map.Entry<String, Object> map: data.entrySet()  ){
            String column = map.getKey();
            columnSql.append(",").append(column);
            valueSql.append(",?");
        }
        columnSql.append(")");
        valueSql.append(")");
        StringBuilder sql = new StringBuilder();
        sql.append(columnSql).append(valueSql);
        ps = conn.prepareStatement(sql.toString());

        int index = 1;
        for (Map.Entry<String, Object> map: data.entrySet()  ){
            String column = map.getKey();
            Object value = map.getValue();
            //判断数据类型
            String columnType = null;
            if(isMysql){
                MySQLColumnVO columnVO = createTablesMapper.queryColumnsInMySQLByColumn(formId, column);
                columnType = MySQLColumnsEnum.getEnumByType(columnVO.getDataType(), columnVO.getCharacterLength());
            }else{
                // do someting in Oracle
            }
            switch (columnType){
                case "Time":
                case "String":
                    String vToC = value.toString();
                    ps.setString(index++, vToC);
                    break;
                case "Blob":
                    ps.setBytes(index++, (byte[]) value);
                    break;
                case "Date":
                    String vToL = value.toString();
                    Date val = null;
                    if (vToL.length() == 10){
                        // 1O位时间戳
                        val = new Date(Integer.parseInt(vToL) * 1000L);
                    }else{
                        val = new Date(Long.parseLong(vToL));
                    }
                    ps.setDate(index++, val);
                    break;
                case "Integer":
                    String vToI = value.toString();
                    ps.setInt(index++, Integer.valueOf(vToI));
                    break;
                case "Double":
                    String vToD = value.toString();
                    ps.setDouble(index++, Double.valueOf(vToD));
                    break;
                default:
                    throw new Exception("数据类型不存在");
            }
        }
        try {
            result = ps.executeUpdate();
        } catch (SQLException e) {
            throw e;
        }finally {
            ps.close();
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }*/
        result = formDataMapper.addFormData(businessKey, formId, data);

        return result;
    }

    public int updateFormData(String businessKey,String formId,Map<String, Object> data) throws Exception {
        int result = 0;
        result = formDataMapper.updateFormData(businessKey, formId, data);
        /*Connection conn = null;
        PreparedStatement ps = null;

        String url = duridConfig.getUrl();
        boolean isMysql = url.contains("mysql");
        conn = jdbcTemplate.getDataSource().getConnection();

        StringBuilder sql = new StringBuilder("update "+ formId + " set ");
        int index = 1;
        for (Map.Entry<String, Object> map: data.entrySet()  ){
            String column = map.getKey();
            sql.append(column).append(" = ").append("?,");
        }
        sql.delete(sql.length() - 1, sql.length()).append(" where business_key = ").append(businessKey);
        ps = conn.prepareStatement(sql.toString());
        for (Map.Entry<String, Object> map: data.entrySet()  ){
            String column = map.getKey();
            Object value = map.getValue();
            //判断数据类型
            String columnType = null;
            if(isMysql){
                MySQLColumnVO columnVO = createTablesMapper.queryColumnsInMySQLByColumn(formId, column);
                columnType = MySQLColumnsEnum.getEnumByType(columnVO.getDataType(), columnVO.getCharacterLength());
            }else{
                // do someting in Oracle
            }
            switch (columnType){
                case "Time":
                case "String":
                    String vToC = value.toString();
                    ps.setString(index++, vToC);
                    break;
                case "Blob":
                    ps.setBytes(index++, (byte[]) value);
                    break;
                case "Date":
                    String vToL = value.toString();
                    Date val = null;
                    if (vToL.length() == 10){
                        // 1O位时间戳
                        val = new Date(Integer.parseInt(vToL) * 1000L);
                    }else{
                        val = new Date(Long.parseLong(vToL));
                    }
                    ps.setDate(index++, val);
                    break;
                case "Integer":
                    String vToI = value.toString();
                    ps.setInt(index++, Integer.valueOf(vToI));
                    break;
                case "Double":
                    String vToD = value.toString();
                    ps.setDouble(index++, Double.valueOf(vToD));
                    break;
                default:
                    throw new Exception("数据类型不存在");
            }
        }
        try {
            result = ps.executeUpdate();
        } catch (SQLException e) {
            throw e;
        }finally {
            ps.close();
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }*/
        return result;
    }


}
